/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.starter.integration.spring.cloud;

import org.pajamas.daydream.template.sequence.Sequence;
import org.pajamas.daydream.template.sequence.SequenceTemplate;
import org.pajamas.daydream.template.sequence.context.MapContext;

import java.util.List;
import java.util.concurrent.CompletionStage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * @author william
 * @since 2024/4/7
 */
public interface GUIDGenerator {
    String SEQUENCE_KEY_SPECIFICATION = "specification";
    String SEQUENCE_KEY_UNIQUE_KEY = "uniqueKey";
    Sequence SEQUENCE_NAMESPACE = SequenceTemplate.CONTEXT_LESS.create("PAJAMAS_GUID_{specification}");
    Sequence SEQUENCE_GUID = SequenceTemplate.CONTEXT_LESS.create("{specification}_{uniqueKey}");

    /**
     * @param specification specification
     * @return java.lang.String
     * @author william
     * @since 2024/4/10
     */
    @Nonnull
    String acquireOne(@Nullable String specification);

    /**
     * @param specification specification
     * @param size          size
     * @return java.util.List<java.lang.String>
     * @author william
     * @since 2024/4/10
     */
    @Nonnull
    List<String> acquireMany(@Nullable String specification, int size);

    /**
     * @param specification specification
     * @return java.util.concurrent.CompletionStage<java.lang.String>
     * @author william
     * @since 2024/4/10
     */
    @Nonnull
    CompletionStage<String> acquireOneAsync(@Nullable String specification);

    /**
     * @param specification specification
     * @param size          size
     * @return java.util.concurrent.CompletionStage<java.util.List < java.lang.String>>
     * @author william
     * @since 2024/4/10
     */
    @Nonnull
    CompletionStage<List<String>> acquireManyAsync(@Nullable String specification, int size);

    /**
     * @param specification specification
     * @return java.lang.String
     * @author william
     * @since 2024/4/10
     */
    @Nonnull
    default String determineNamespace(@Nullable String specification) {
        return SEQUENCE_NAMESPACE.interpret(new MapContext(SEQUENCE_KEY_SPECIFICATION, specification));
    }

    /**
     * @param specification specification
     * @param uniqueKey     uniqueKey
     * @return java.lang.String
     * @author william
     * @since 2024/4/10
     */
    @Nonnull
    default String fillGUID(@Nullable String specification, @Nonnull Object uniqueKey) {
        MapContext context = new MapContext();
        context.put(SEQUENCE_KEY_SPECIFICATION, specification);
        context.put(SEQUENCE_KEY_UNIQUE_KEY, uniqueKey);
        return SEQUENCE_GUID.interpret(context);
    }
}
